
define(['echarts'], function (require, exports) {
	/**
	 * 页面加载完成时触发
	 * @param hashCode 路由ID
	 * @param data 传递数据对象
	 * @param cite 页面站点信息
	 */
	exports.ready = function (hashCode, data, cite) {
		yufp.lookup.reg('USE_TASK_RUN_STATE,USE_JOB_RUN_STATE');
		yufp.custom.vue({
			el: cite.el,
			data: function () {

				var errorFlagOption = [{ key: 'N', value: '成功' }, { key: 'Y', value: '失败' }];

				var statusOption = [{ key: 'Running', value: '运行中' }, { key: 'Interrupting', value: '中断中' }, { key: 'Interrupted', value: '被中断' }, { key: 'S', value: '已停止' },
				{ key: 'Stopped', value: '已结束' }, { key: 'Finished', value: '已完成' }, { key: 'Debugging', value: '测试中' }, { key: 'Preparing', value: '准备中' }]

				var _this = this;

				return {
					tbHeight:'0',
					customKey: '',
					routeId: '',
					searchFormdata: {},
					appNameDisabled: false,
					taskNameDisabled: false,
					treeDataUrl: "api/use/monitor/taskMonitor/queryScheduleHisTreeList",
					dataUrl: 'api/use/monitor/taskMonitorHis/queryList',
					flowsheetShow: false,
					rerunShow: false,
					statusOption: statusOption,
					formdata: {},
					async: false,
					param: { UNITID: '0000', LEVELUNIT: '1' },
					height: yufp.frame.size().height - 115,
					errorFlagOption: errorFlagOption,
					viewTitleArrry: ['普通事件监控'],
					viewTitle: '',
					isAppNode: false,
					isNodeLeaf: false,
					nodeLeafData: {},
					currAppNode: {},
					currNodeData: {},
					mainTag: 'tasks',
					historyTaskLogTag: 'historyTasksLog',
					queryName: "",
					queryStaticsFields: [
						{ placeholder: '开始日期', field: 'beginDate', type: 'date', format: "yyyyMMdd" },
						{ placeholder: '结束日期', field: 'endDate', type: 'date', format: "yyyyMMdd" },

					],

					queryStaticsButtons: [
						{
							label: '搜索', op: 'submit', type: 'primary', click: function (model, valid) {

								let startDate = "";
								let endDate = "";

								if (model.beginDate != null && model.beginDate != '') {
									startDate = _this.formartCycleDate(model.beginDate);

								}

								if (model.endDate != null && model.endDate != '') {
									endDate = _this.formartCycleDate(model.endDate);

								}

								_this.queryTaskHisInfo(startDate, endDate);

							}
						},
						{ label: '重置', op: 'reset' }
					],
					//排序字段映射
					sortColunms: {
						'appName': 'APP_NAME',
						'taskName': 'TASK_NAME',
						'cycleTime': 'CYCLE_TIME',
						'errorFlag': 'ERROR_FLAG'
					},
					//排序方式映射
					sortTypes: {
						'descending': 'desc',
						'ascending': 'asc'
					}
				};
			},
			mounted:function(){
				var _this = this;
				//测试
				_this.$nextTick(function(){
						var tableBox = _this.$refs.tableBox;
						let a = tableBox.getBoundingClientRect().top + 28;
						var theight = `calc(100vh - ${a}px - 64px)`;
						_this.tbHeight = theight;		
				});
			},
			methods: {
				showFlowChart: function (row) {
					var _this = this;

					// 判断是否已有页签id
					if (_this.customKey == '') {
						_this.customKey = 'custom_' + new Date().getTime();
						_this.routeId = 'router_' + new Date().getTime();
						var url = 'pages/use/monitors/taskMonitor/taskMonitorGraph/taskMonitorGraph';
						yufp.router.addRoute(_this.routeId, {
							html: url + '.html',
							js: url + '.js'
						});
					}

					// 获取当前页面是否有标签页
					var newtab = yufp.frame.getTab(_this.customKey);
					if (newtab == undefined) {//没有则使用routeId和customKey生成新的Tab
						yufp.frame.addTab({
							id: _this.routeId, // 菜单功能ID（路由ID）
							key: _this.customKey, // 自定义唯一页签key,请统一使用custom_前缀开头
							title: row.taskName + '_流程图', // 页签名称
							data: yufp.clone({ isHistory: true }, row)// 传递的业务数据，可选配置
						});
					} else {//有则删除原有tab页再生成
						//删除原有tab
						yufp.frame.removeTab(_this.customKey);
						//生成新的页签id
						_this.customKey = 'custom_' + new Date().getTime();
						//移除原有路由
						yufp.router.removeRoute(_this.routeId);
						//注册新的路由
						_this.routeId = 'router_' + new Date().getTime();
						var url = 'pages/use/monitors/taskMonitor/taskMonitorGraph/taskMonitorGraph';
						yufp.router.addRoute(_this.routeId, {
							html: url + '.html',
							js: url + '.js'
						});
						//生成新页签
						yufp.frame.addTab({
							id: _this.routeId, // 菜单功能ID（路由ID）
							key: _this.customKey, // 自定义唯一页签key,请统一使用custom_前缀开头
							title: row.taskName + '_流程图', // 页签名称
							data: yufp.clone({ isHistory: true }, row)// 传递的业务数据，可选配置
						});
					}
				},

				selectTask: function (selection) {
					var _this = this;
					if (selection[0] == null) {
						_this.rerunShow = false;
						_this.flowsheetShow = false;
					} else {
						_this.flowsheetShow = true;
						_this.rerunShow = true;
					}
				},
				/**
				 * 点击树的结点，加载对应的数据
				 */
				nodeClickFn: function (nodeData, node, self) {
					var _this = this;
					var taskId = "";
					var appCode = "";
					_this.currNodeData = nodeData;

					if (nodeData.nodeType === 'A') {
						appCode = nodeData.appCode;
						_this.isAppNode = true;
						_this.isNodeLeaf = false;
						_this.appNameDisabled = true;
						_this.$refs.wftRunForm.resetFields();
						_this.searchFormdata.appName = nodeData.currentNodeName;
						_this.taskNameDisabled = false
						//_this.searchFormdata.taskName = '';

					} else if (nodeData.nodeType === 'D') {
						appCode = nodeData.appCode;
						taskId = nodeData.currentNodeId;
						_this.isNodeLeaf = true;
						_this.isAppNode = false;
						_this.appNameDisabled = true;
						_this.$refs.wftRunForm.resetFields();
						_this.searchFormdata.appName = nodeData.parentName;
						_this.taskNameDisabled = true
						_this.searchFormdata.taskName = nodeData.currentNodeName;
					} else {
						_this.isNodeLeaf = false;
						_this.isAppNode = false;
						_this.appNameDisabled = false;
						_this.$refs.wftRunForm.resetFields();
						_this.searchFormdata.appName = '';
						_this.taskNameDisabled = false
						_this.searchFormdata.taskName = '';
					}
					_this.historyTaskLogTag = "historyTasksLog";

					_this.searchFormdata.appCode = appCode;
					_this.searchFormdata.taskId = taskId;

					var param = { condition: JSON.stringify(_this.searchFormdata) };
					_this.$refs.refTable.remoteData(param);


				},

				formartCycleDate: function (val) {
					if (val == null || val == '') {
						return "";
					}
					var d = new Date(val);
					var year = d.getFullYear();
					var month = (d.getMonth() + 1) < 10 ? '0' + (d.getMonth() + 1) : (d.getMonth() + 1);
					var date = d.getDate() < 10 ? '0' + d.getDate() : d.getDate();

					return year + "" + month + "" + date;
				},

				queryTaskHisInfo: function (startDate, endDate) {
					var _this = this;
					var param = {
						appCode: _this.currNodeData.appCode, groupCode: _this.currNodeData.currentNodeId,
						queryStartDate: startDate, queryEndDate: endDate
					};
					yufp.service.request({
						url: backend.useService + '/monitor/taskMonitorHis/queryHisLineChart',
						method: 'get',
						data: param,
						callback: function (code, message, response) {
							if (response && response.data) {
								_this.$nextTick(function () {
									_this.initChart(response.data);
								});
							}
						}
					});
				},

				initChart: function (data) {
					var taskHisLineChart = echarts.init(document.getElementById("histaskLineChart"));
					taskHisLineChart.setOption(this.getLineOption(data));
				},

				showHisLineChart: function () {
					if (this.historyTaskLogTag === 'statisticTasksLog') {
						let startDate = "";
						let endDate = "";

						if (this.$refs.staticJobRef.fm.beginDate != null && this.$refs.staticJobRef.fm.beginDate != '') {
							startDate = this.formartCycleDate(this.$refs.staticJobRef.fm.beginDate);
						}

						if (this.$refs.staticJobRef.fm.endDate != null && this.$refs.staticJobRef.fm.endDate != '') {
							endDate = this.formartCycleDate(this.$refs.staticJobRef.fm.endDate);
						}

						this.queryTaskHisInfo(startDate, endDate);
					}
				},



				getLineOption: function (data) {
					return {
						title: {
							text: '任务名称:' + this.currNodeData.currentNodeName,
							x: 'center',
							y: 'top',
						},

						tooltip: {
							trigger: 'axis',
							formatter: '周期时点: {b}<br/> ' +
								'{a0}:{c0}秒'
						},

						xAxis: {
							type: 'category',
							boundaryGap: false,
							data: data.xAxisStrList,
							show: true,
							axisLabel: {
								show: true,
								interval: 'auto',
								formatter: '{value}'
							},
						},
						yAxis: {
							type: 'value',
							axisLabel: {
								show: true,
								interval: 'auto',
								formatter: '{value}秒'
							},
							// show: true
						},
						series: [

							{
								name: '平均耗时',
								type: 'line',
								smooth: true,
								data: data.datas,
							}
						]
					};
				},
				showTask: function (row) {
					var customKey = 'custom_' + new Date().getTime();
					var routeId = 'router_' + new Date().getTime();
					var url = 'pages/use/monitors/taskMonitor/taskMonitorList/taskMonitorList';
					yufp.router.addRoute(routeId, {
						html: url + '.html',
						js: url + '.js'
					});
					row.customKey = customKey;
					row.isHistory = true;
					yufp.frame.addTab({
						id: routeId, // 菜单功能ID（路由ID）
						key: customKey, // 自定义唯一页签key,请统一使用custom_前缀开头
						// title: row.appName + '_流程图', // 页签名称
						title: '[' + row.taskName + ']历史作业清单', // 页签名称
						data: yufp.clone({ routeId: routeId, title: '[' + row.taskName + ']作业清单' }, row)// 传递的业务数据，可选配置
					});
				},
				/**
				 * 排序
				*/
				wftRunSort: function (column) {
					var _this = this
					_this.searchFormdata.sortColunm = _this.sortColunms[column.prop];
					_this.searchFormdata.sorType = _this.sortTypes[column.order];
					var param = { condition: JSON.stringify(_this.searchFormdata) };
					_this.$refs.refTable.remoteData(param);
				}

			},

		});
	};
});